package main

import (
	"fmt"
)

type HeroNode struct {
	No       int
	Pre      *HeroNode
	Next     *HeroNode
	Name     string
	NickName string
}

func AppendNode(head *HeroNode, newHeoNode *HeroNode) {
	temp := head
	for {
		if temp.Next == nil {
			break
		}
		temp = temp.Next
	}
	temp.Next = newHeoNode
}

func InsertNodeByNo(head *HeroNode, newHeoNode *HeroNode) {
	temp := head
	flag := true
	for {
		if temp.Next == nil {
			break
		}
		if temp.Next.No > newHeoNode.No {
			break
		} else if temp.Next.No == newHeoNode.No {
			flag = false
			break
		}
		temp = temp.Next
	}
	if !flag {
		fmt.Println("节点no已存在")
		return
	} else {
		newHeoNode.Next = temp.Next
		temp.Next = newHeoNode
	}
}

func InsertNodeByNo2(head *HeroNode, newNode *HeroNode) {
	temp := head
	flag := false
	for {
		//检索到最后一个节点直接退出循环
		if temp.Next == nil {
			break
		}
		//否则非最后一个节点
		if temp.Next.No >= newNode.No {
			newNode.Next = temp.Next
			temp.Next.Pre = newNode

			temp.Next = newNode
			newNode.Pre = temp
			flag = true
			break
		}
		temp = temp.Next
	}
	if !flag {
		temp.Next = newNode
		newNode.Pre = temp
	}



	//双向 改变前
	// 1.0 <- 3.0
	// 1.1 -> 3.1

	//双向 改变后
	// 1.0 <- 2.0 <- 3.0
	// 1.1 -> 2.1 -> 3.1

	//temp newNode TempNext

	//newNode.Next = temp.Next
	//temp.Next = newNode
	//
	//newNode.Pre = temp
	//newNode.Next.Pre = newNode
}

func PrintNode(head *HeroNode) {
	temp := head
	if temp.Next == nil {
		fmt.Println("空空如也")
		return
	}
	for {
		fmt.Printf("[%v,%v,%v]==>", temp.Next.No, temp.Next.Name, temp.Next.NickName)
		temp = temp.Next
		if temp.Next == nil {
			break
		}
	}
}

func PrintNode2(head *HeroNode) {
	temp := head
	if temp.Next == nil {
		fmt.Println("空空如也")
		return
	}
	for {
		if temp.Next == nil {
			break
		}
		temp = temp.Next
	}
	for {
		if temp.Pre == nil {
			break
		}
		fmt.Printf("[%v,%v,%v]==>", temp.No, temp.Name, temp.NickName)
		temp = temp.Pre
	}
}

func DeleteNode(head *HeroNode, no int) {
	temp := head
	flag := false
	for {
		if temp.Next == nil {
			break
		}
		if temp.Next.No == no {
			flag = true
			break
		}
		temp = temp.Next
	}
	if !flag {
		fmt.Println("节点no未找到")
		return
	} else {
		//1 ->(next=2.0)
		//2 ->(next=3.0)
		//3 ->(next=nil)
		//删掉 2节点
		//1 ->(next=1.next.next)

		temp.Next = temp.Next.Next
	}
}

func DeleteNode2(head *HeroNode, no int) {
	temp := head
	flag := false
	for {
		if temp.Next == nil {
			break
		}
		if temp.Next.No == no {
			flag = true
			break
		}
		temp = temp.Next
	}
	if !flag {
		fmt.Println("节点no未找到")
		return
	} else {
		//1 ->(next=2.0)
		//2 ->(next=3.0)
		//3 ->(next=nil)
		//删掉 2节点
		//1 ->(next=1.next.next)
		temp.Next = temp.Next.Next

		//1 ->(pre=nil)
		//2 ->(pre=1.0)
		//3 ->(pre=2.0)
		//删掉 2节点
		//3 ->(1.Next.Next = 1)
		if temp.Next != nil {
			temp.Next.Pre = temp
		}
	}
}